
enum BBCT {
    //% block="现在的温度是"
    1,
    //% block="度"
    2,
    //% block="现在的湿度是百分之"
    3,
    //% block="一"
    4,
    //% block="二"
    5,
    //% block="三"
    6,
    //% block="四"
    7,
    //% block="五"
    8,
    //% block="六"
    9,
    //% block="七"
    10,
    //% block="八"
    11,
    //% block="九"
    12,
    //% block="十"
    13,
    //% block="个"
    14,
    //% block="百"
    15,
    //% block="千"
    16,
    //% block="万"
    17,
    //% block="亿"
    18,
    //% block="点"
    19,
    //% block="负"
    20,
    //% block="零"
    21,
    //% block="现在的时间是"
    22,
    //% block="年"
    23,
    //% block="月"
    24,
    //% block="日"
    25,
    //% block="时"
    26,
    //% block="分"
    27,
    //% block="秒"
    28,
    //% block="百分之"
    29,
    //% block="现在的距离是"
    30,
    //% block="毫米"
    31,
    //% block="厘米"
    32,
    //% block="米"
    33,
    //% block="千米"
    33
}


enum CK {
    //% block="软串口"
    1,
    //% block="硬串口"
    2
}
enum SBC {
    //% block="开灯"
    1,
    //% block="关灯"
    2,
    //% block="调为红色"
    3,
    //% block="调为蓝色"
    4,
    //% block="调为绿色"
    5,
    //% block="调为彩色"
    6,
    //% block="打开空调"
    7,
    //% block="关闭空调"
    8,
    //% block="打开风扇"
    9,
    //% block="关闭风扇"
    16,
    //% block="1档风"
    17,
    //% block="2档风"
    18,
    //% block="3档风"
    19,
    //% block="自动模式"
    20,
    //% block="制冷模式"
    21,
    //% block="制热模式"
    22,
    //% block="升高温度"
    23,
    //% block="降低温度"
    24,
    //% block="打开台灯"
    25,
    //% block="关闭台灯"
    32,
    //% block="调到最亮"
    33,
    //% block="调到最暗"
    34,
    //% block="调亮一点"
    35,
    //% block="调暗一点"
    36,
    //% block="查询温度"
    37,
    //% block="查询湿度"
    38,
    //% block="查询天气"
    39,
    //% block="前进"
    40,
    //% block="后退"
    41,
    //% block="左转"
    48,
    //% block="右转"
    49,
    //% block="停止"
    50,
    //% block="查询时间"
    51,
    //% block="播放音乐"
    52,
    //% block="上一首"
    53,
    //% block="下一首"
    54,
    //% block="开始"
    55,
    //% block="暂停"
    56     
}



//% color="#AA278D" iconWidth=50 iconHeight=40
namespace su03t {
    

    //% block="初始化语音识别模块 [CK]RX为[RX]TX为[TX]" blockType="command"
    //% RX.shadow="dropdown" RX.options="PIN_DigitalWrite" 
    //% TX.shadow="dropdown" TX.options="PIN_DigitalWrite"  
    //% CK.shadow="dropdownRound" CK.options="CK" CK.defl="CK.1"
    export function yuyin_init(parameter: any, block: any) {
        let RX = parameter.RX.code
        let TX = parameter.TX.code
        let model = parameter.CK.code
        if(model==1)
        {
        Generator.addInclude('yuyin_init_1', '#include <SoftwareSerial.h>');
        Generator.addDefine('UART_SEND_MAX 32', " ")
        Generator.addDefine('UART_MSG_HEAD_LEN 2', " ")
        Generator.addDefine(' UART_MSG_FOOT_LEN  2', " ")
        Generator.addDefine(' U_MSG_bozhensgshu      1', " ")
        Generator.addDefine(' U_MSG_boxiaoshu      2', " ")
        Generator.addDefine(' U_MSG_bobao1      3', " ")
        Generator.addDefine(' U_MSG_bobao2      4', " ")
        Generator.addDefine(' U_MSG_bobao3      5', " ")
        Generator.addDefine(' U_MSG_bobao4      6', " ")
        Generator.addDefine(' U_MSG_bobao5      7', " ")
        Generator.addDefine(' U_MSG_bobao6      8', " ")
        Generator.addDefine(' U_MSG_bobao7      9', " ")
        Generator.addDefine(' U_MSG_bobao8      10', " ")
        Generator.addDefine(' U_MSG_bobao9      11', " ")
        Generator.addDefine(' U_MSG_bobao10      12', " ")
        Generator.addDefine(' U_MSG_bobao11      13', " ")
        Generator.addDefine(' U_MSG_bobao12      14', " ")
        Generator.addDefine(' U_MSG_bobao13      15', " ")
        Generator.addDefine(' U_MSG_bobao14      16', " ")
        Generator.addDefine(' U_MSG_bobao15      17', " ")
        Generator.addDefine(' U_MSG_bobao16      18', " ")
        Generator.addDefine(' U_MSG_bobao17      19', " ")
        Generator.addDefine(' U_MSG_bobao18      20', " ")
        Generator.addDefine(' U_MSG_bobao19      21', " ")
        Generator.addDefine(' U_MSG_bobao20      22', " ")
        Generator.addDefine(' U_MSG_bobao21      23', " ")
        Generator.addDefine(' U_MSG_bobao22      24', " ")
        Generator.addDefine(' U_MSG_bobao23      25', " ")
        Generator.addDefine(' U_MSG_bobao24      26', " ")
        Generator.addDefine(' U_MSG_bobao25      27', " ")
        Generator.addDefine(' U_MSG_bobao26      28', " ")
        Generator.addDefine(' U_MSG_bobao27      29', " ")
        Generator.addDefine(' U_MSG_bobao28      30', " ")
        Generator.addDefine(' U_MSG_bobao29      31', " ")
        Generator.addDefine(' U_MSG_bobao30      32', " ")
        Generator.addDefine(' U_MSG_bobao31      33', " ")
        Generator.addDefine(' U_MSG_bobao32      34', " ")
        Generator.addDefine(' U_MSG_bobao33      35', " ")
        Generator.addDefine(' U_MSG_bobao34      36', " ")
        Generator.addObject(`yuyin_init_3`, `volatile int yuyin;`,` `);//会自动在最后加上;生成在声明的地方
        Generator.addObject(`yuyin_init_4`, `typedef union {
            double d_double;
            int d_int;
            unsigned char d_ucs[8];
            char d_char;
            unsigned char d_uchar;
            unsigned long d_long;
            short d_short;
            float d_float;
        }uart_param_t`,` `);
        Generator.addObject(`yuyin_init_5`, `SoftwareSerial mySerial(${RX},${TX});`,` `);
        Generator.addObject(`yuyin_init_6`, `void _uart_send_impl(unsigned char* buff, int len) {
            for(int i=0;i<len;i++)
          {
             mySerial.write (*buff++);
          }
          }
          
          void shuaxin(){
          if (mySerial.available() > 0) {
          yuyin = mySerial.read();
           Serial.println(yuyin,HEX);
          }
          }
          // 串口通信消息尾
          const unsigned char g_uart_send_foot[] = {
            0x55, 0xaa
          };
          
          // 十六位整数转32位整数
          void _int16_to_int32(uart_param_t* param) {
            if (sizeof(int) >= 4)
              return;
            unsigned long value = param->d_long;
            unsigned long sign = (value >> 15) & 1;
            unsigned long v = value;
            if (sign)
              v = 0xFFFF0000 | value;
            uart_param_t p;  p.d_long = v;
            param->d_ucs[0] = p.d_ucs[0];
            param->d_ucs[1] = p.d_ucs[1];
            param->d_ucs[2] = p.d_ucs[2];
            param->d_ucs[3] = p.d_ucs[3];
          }
          
          // 浮点数转双精度
          void _float_to_double(uart_param_t* param) {
            if (sizeof(int) >= 4)
              return;
            unsigned long value = param->d_long;
            unsigned long sign = value >> 31;
            unsigned long M = value & 0x007FFFFF;
            unsigned long e =  ((value >> 23 ) & 0xFF) - 127 + 1023;
            uart_param_t p0, p1;
            p1.d_long = ((sign & 1) << 31) | ((e & 0x7FF) << 20) | (M >> 3);
            param->d_ucs[0] = p0.d_ucs[0];
            param->d_ucs[1] = p0.d_ucs[1];
            param->d_ucs[2] = p0.d_ucs[2];
            param->d_ucs[3] = p0.d_ucs[3];
            param->d_ucs[4] = p1.d_ucs[0];
            param->d_ucs[5] = p1.d_ucs[1];
            param->d_ucs[6] = p1.d_ucs[2];
            param->d_ucs[7] = p1.d_ucs[3];
          }
          
          // 串口通信消息头
          const unsigned char g_uart_send_head[] = {
            0xaa, 0x55
          }`,` `);
        //Generator.addEvent("functionName", "String", "functionName", "String message, int8_t error", true);
        Generator.addSetup(`yuyin_init_7`, `  mySerial.begin(9600);
  yuyin = 0;`);
       //Generator.addCode(`myoled.setCursorLine(${line});\n\tmyoled.printLine(${str});`);
        }
        else
        {
            Generator.addInclude('yuyin_init_1', '#include <HardwareSerial.h>');
        Generator.addDefine('UART_SEND_MAX 32', " ")
        Generator.addDefine('UART_MSG_HEAD_LEN 2', " ")
        Generator.addDefine(' UART_MSG_FOOT_LEN  2', " ")
        Generator.addDefine(' U_MSG_bozhensgshu      1', " ")
        Generator.addDefine(' U_MSG_boxiaoshu      2', " ")
        Generator.addDefine(' U_MSG_bobao1      3', " ")
        Generator.addDefine(' U_MSG_bobao2      4', " ")
        Generator.addDefine(' U_MSG_bobao3      5', " ")
        Generator.addDefine(' U_MSG_bobao4      6', " ")
        Generator.addDefine(' U_MSG_bobao5      7', " ")
        Generator.addDefine(' U_MSG_bobao6      8', " ")
        Generator.addDefine(' U_MSG_bobao7      9', " ")
        Generator.addDefine(' U_MSG_bobao8      10', " ")
        Generator.addDefine(' U_MSG_bobao9      11', " ")
        Generator.addDefine(' U_MSG_bobao10      12', " ")
        Generator.addDefine(' U_MSG_bobao11      13', " ")
        Generator.addDefine(' U_MSG_bobao12      14', " ")
        Generator.addDefine(' U_MSG_bobao13      15', " ")
        Generator.addDefine(' U_MSG_bobao14      16', " ")
        Generator.addDefine(' U_MSG_bobao15      17', " ")
        Generator.addDefine(' U_MSG_bobao16      18', " ")
        Generator.addDefine(' U_MSG_bobao17      19', " ")
        Generator.addDefine(' U_MSG_bobao18      20', " ")
        Generator.addDefine(' U_MSG_bobao19      21', " ")
        Generator.addDefine(' U_MSG_bobao20      22', " ")
        Generator.addDefine(' U_MSG_bobao21      23', " ")
        Generator.addDefine(' U_MSG_bobao22      24', " ")
        Generator.addDefine(' U_MSG_bobao23      25', " ")
        Generator.addDefine(' U_MSG_bobao24      26', " ")
        Generator.addDefine(' U_MSG_bobao25      27', " ")
        Generator.addDefine(' U_MSG_bobao26      28', " ")
        Generator.addDefine(' U_MSG_bobao27      29', " ")
        Generator.addDefine(' U_MSG_bobao28      30', " ")
        Generator.addDefine(' U_MSG_bobao29      31', " ")
        Generator.addDefine(' U_MSG_bobao30      32', " ")
        Generator.addDefine(' U_MSG_bobao31      33', " ")
        Generator.addDefine(' U_MSG_bobao32      34', " ")
        Generator.addDefine(' U_MSG_bobao33      35', " ")
        Generator.addDefine(' U_MSG_bobao34      36', " ")
        Generator.addObject(`yuyin_init_3`, `volatile int yuyin;`,` `);//会自动在最后加上;生成在声明的地方
        Generator.addObject(`yuyin_init_4`, `typedef union {
            double d_double;
            int d_int;
            unsigned char d_ucs[8];
            char d_char;
            unsigned char d_uchar;
            unsigned long d_long;
            short d_short;
            float d_float;
        }uart_param_t`,` `);
        //Generator.addObject(`yuyin_init_5`, `SoftwareSerial mySerial(${RX},${TX});`,` `);
        Generator.addObject(`yuyin_init_6`, `void _uart_send_impl(unsigned char* buff, int len) {
            for(int i=0;i<len;i++)
            {
               Serial2.write (*buff++);
            }
          }
          
          void shuaxin(){
            if (Serial2.available() > 0) {
                yuyin = Serial2.read();
                 Serial.println(yuyin,HEX);
                }
          }
          // 串口通信消息尾
          const unsigned char g_uart_send_foot[] = {
            0x55, 0xaa
          };
          
          // 十六位整数转32位整数
          void _int16_to_int32(uart_param_t* param) {
            if (sizeof(int) >= 4)
              return;
            unsigned long value = param->d_long;
            unsigned long sign = (value >> 15) & 1;
            unsigned long v = value;
            if (sign)
              v = 0xFFFF0000 | value;
            uart_param_t p;  p.d_long = v;
            param->d_ucs[0] = p.d_ucs[0];
            param->d_ucs[1] = p.d_ucs[1];
            param->d_ucs[2] = p.d_ucs[2];
            param->d_ucs[3] = p.d_ucs[3];
          }
          
          // 浮点数转双精度
          void _float_to_double(uart_param_t* param) {
            if (sizeof(int) >= 4)
              return;
            unsigned long value = param->d_long;
            unsigned long sign = value >> 31;
            unsigned long M = value & 0x007FFFFF;
            unsigned long e =  ((value >> 23 ) & 0xFF) - 127 + 1023;
            uart_param_t p0, p1;
            p1.d_long = ((sign & 1) << 31) | ((e & 0x7FF) << 20) | (M >> 3);
            param->d_ucs[0] = p0.d_ucs[0];
            param->d_ucs[1] = p0.d_ucs[1];
            param->d_ucs[2] = p0.d_ucs[2];
            param->d_ucs[3] = p0.d_ucs[3];
            param->d_ucs[4] = p1.d_ucs[0];
            param->d_ucs[5] = p1.d_ucs[1];
            param->d_ucs[6] = p1.d_ucs[2];
            param->d_ucs[7] = p1.d_ucs[3];
          }
          
          // 串口通信消息头
          const unsigned char g_uart_send_head[] = {
            0xaa, 0x55
          }`,` `);
        //Generator.addEvent("functionName", "String", "functionName", "String message, int8_t error", true);
        Generator.addSetup(`yuyin_init_7`, `  Serial2.begin(9600,SERIAL_8N1,${RX},${TX});
  yuyin = 0;`);
       //Generator.addCode(`myoled.setCursorLine(${line});\n\tmyoled.printLine(${str});`);
        }
    }

    
    //% block="识别到[SBC1]?" blockType="boolean"
    //% Flag.shadow="boolean"
    //% SBC1.shadow="dropdown" SBC1.options="SBC" SBC1.defl="SBC.开灯"
    export function shibieyyc(parameter: any, block: any) {
        let YYSBC =parameter.SBC1.code;
            Generator.addCode(`yuyin ==${YYSBC}`);

    }
    //% block="清除识别结果" blockType="command"
    export function qcshibie(parameter: any, block: any) {
            Generator.addCode(`yuyin = 0;`);

    }
    //% block="刷新识别结果" blockType="command"
    export function sxshibie(parameter: any, block: any) {
        Generator.addCode(`shuaxin();`);
    }
    //% block="播放小数[SX]" blockType="command"
    //% SX.shadow="number"  SX.defl=12.34
    export function bfxiaoshu(parameter: any, block: any) {
        let xiaoshu=parameter.SX.code
        Generator.addObject(`bfxiaoshu_1`, `void _uart_boxiaoshu`,`(float xiaoshu) {
            uart_param_t param;
              int i = 0;
              unsigned char buff[UART_SEND_MAX] = {0};
              for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
                  buff[i + 0] = g_uart_send_head[i];
              }
              buff[2] = U_MSG_boxiaoshu;
              param.d_double = xiaoshu;
              _float_to_double(&param);
              buff[3] = param.d_ucs[0];
              buff[4] = param.d_ucs[1];
              buff[5] = param.d_ucs[2];
              buff[6] = param.d_ucs[3];
              buff[7] = param.d_ucs[4];
              buff[8] = param.d_ucs[5];
              buff[9] = param.d_ucs[6];
              buff[10] = param.d_ucs[7];
              for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
                  buff[i + 11] = g_uart_send_foot[i];
              }
              _uart_send_impl(buff, 13);
          }`);
        Generator.addCode(`_uart_boxiaoshu(${xiaoshu});`);
    }
    //% block="播放整数[ZS]" blockType="command"
    //% ZS.shadow="number"  ZS.defl=25
    export function bfzhengshu(parameter: any, block: any) {
        let zhengshu=parameter.ZS.code
        Generator.addObject(`bfzhengshu_1`, `void _uart_bozhensgshu`,`(int zhengshu) {
            uart_param_t param;
              int i = 0;
              unsigned char buff[UART_SEND_MAX] = {0};
              for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
                  buff[i + 0] = g_uart_send_head[i];
              }
              buff[2] = U_MSG_bozhensgshu;
              param.d_int = zhengshu;
              _int16_to_int32(&param);
              buff[3] = param.d_ucs[0];
              buff[4] = param.d_ucs[1];
              buff[5] = 0;
              buff[6] = 0;
              for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
                  buff[i + 7] = g_uart_send_foot[i];
              }
              _uart_send_impl(buff, 9);
          }`);
        Generator.addCode(`_uart_bozhensgshu(${zhengshu});`);
    }
    //% block="播报[bbct]" blockType="command"
    //% bbct.shadow="dropdownRound" bbct.options="BBCT" bbct.defl="BBCT.现在的温度是"
    export function bobaoct(parameter: any, block: any) {
        let BBNR = parameter.bbct.code
            Generator.addObject(`bobaoct_${BBNR}`, `void _uart_bobao${BBNR}`,`() {
                uart_param_t param;
                  int i = 0;
                  unsigned char buff[UART_SEND_MAX] = {0};
                  for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
                      buff[i + 0] = g_uart_send_head[i];
                  }
                  buff[2] = U_MSG_bobao${BBNR};
                  for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
                      buff[i + 3] = g_uart_send_foot[i];
                  }
                  _uart_send_impl(buff, 5);
              }`);
            Generator.addCode(`_uart_bobao${BBNR}();`);
        }
}
